﻿@inject ImagesLargeViewService ImagesLargeViewService
@implements IDisposable


@if (isOpen)
{
    <style>
        body {
            overflow: hidden;
        }
    </style>
    <div class="largeimage-bg">
        <div class="largeimage-layout">
            <div class="d-flex p-3 largeimage-layout-in">
                <div class="ps-3 pe-3 largeimage-layout-img">
                    <a href="@Model[_index].Image" target="_blank" class="largeimage-a ">
                        <img class="largeimage-img" src="@Model[_index].Image" alt="@Model[_index].Note" />
                    </a>
                </div>
            </div>
            <p class="text-center m-0" style="line-break: anywhere; font-size: medium;color:white">@Model[_index].Note</p>
            <p class="text-center mb-0" style=" line-break: anywhere; font-size: x-large; font-weight: bold; color: white;">@((_index + 1).ToString() + " / " + Model.Count.ToString())</p>
        </div>
        <div class="largeimage-btn-close">
            <CnGalWebSite.Components.Buttons.MasaButton Tooltip="关闭" Fab Icon="@IconType.Close.ToIconString()" OnClick="OnClose" />
         </div>                <div class="largeimage-btn-left">
             <CnGalWebSite.Components.Buttons.MasaButton Tooltip="上一张" Fab Icon="@IconType.Left.ToIconString()" OnClick="OnMoveLeft" />
         </div>
         <div class="largeimage-btn-right">
             <CnGalWebSite.Components.Buttons.MasaButton Tooltip="下一张" Fab Icon="@IconType.Right.ToIconString()" OnClick="OnMoveRight" />
         </div>
     </div>
}


@code {

    [Parameter]
    public List<ImagesLargeViewModel> Model { get; set; } = new List<ImagesLargeViewModel>();

    int _index;

    private bool isOpen = false;

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        ImagesLargeViewService.Register(this, ViewLargeImages);
    }

    public void Blank()
    {

    }

    /// <summary>
    /// 设置网站 Title 方法
    /// </summary>
    /// <param name="title"></param>
    /// <returns></returns>
    private ValueTask ViewLargeImages(List<ImagesLargeViewModel> model,int index)
    {
        if (model == null || model.Count == 0)
        {
            return ValueTask.CompletedTask;
        }
        Model = model;
        _index = index;
        isOpen = true;
        StateHasChanged();
        return ValueTask.CompletedTask;
    }

    public void OnMoveLeft()
    {
        _index = (_index - 1) < 0 ? (Model.Count - 1) : (_index - 1);
        StateHasChanged();

    }

    public void OnMoveRight()
    {
        _index = (_index + 1) >= Model.Count ? 0 : (_index + 1);
        StateHasChanged();

    }

    public void OnClose()
    {
        isOpen = false;
        StateHasChanged();

    }

    /// <summary>
    /// Dispose 方法
    /// </summary>
    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            ImagesLargeViewService.UnRegister(this);
        }
    }

    /// <summary>
    /// Dispose 方法
    /// </summary>
    public void Dispose()
    {
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
}
